package kr.hkit.powerjava.chapter22;

import java.util.Arrays;

public class Array {
	public static <T> T getLast(T[] a){
		return a[a.length-1];
	}
	
	public static <T extends Comparable<T>> T getMax(T[] a){
		if(a == null || a.length ==0) return null;
		
		T largest = a[0];
		for (int i = 1; i < a.length; i++) {
			if(largest.compareTo(a[i]) < 0){
				largest = a[i];
				
			}
		}
		return largest;
	}
	
	public static < T extends Comparable<T>> T getMin(T[] a){
		if(a == null || a.length ==0)return null;
		
		T smallest = a[0];
		for (int i = 1; i < a.length; i++) {
			if(smallest.compareTo(a[i])>0){
				smallest = a[i];
			}
		}
		return smallest;
	}
	
	public static void main(String[] args) {
		String[] language = { "AVA","C#" , "C++", "BASIC"};
		Integer[] arr = {5,4,3,2,1};					//래퍼클래스만 가능하다 int[] xx
		
		String last = Array./*<String> 생략 */getLast(language);
		System.out.println(last);
	
		int aLast = Array.getLast(arr);
		System.out.println(aLast);
		
		int aMax=Array.getMax(arr);
		int aMin=Array.getMin(arr);
		System.out.println(Arrays.toString(arr) + "의 배열에서 가장 큰 값은 ? " + aMax);
		System.out.println(Arrays.toString(arr) + "의 배열에서 가장 장은 값은 ? " + aMin);
	
		Test[] arrTest = new Test[3];
		arrTest[0] = new Test(5);
		arrTest[1] = new Test(4);
		arrTest[2] = new Test(3);
		
		System.out.println(Arrays.toString(arrTest) + Array.getMax(arrTest));
		System.out.println(Arrays.toString(arrTest) + Array.getMin(arrTest));
	}	
}


class Test implements Comparable<Test>{
	private int a;

	
	
	public Test(int a) {
		this.a = a;
	}

	public int getA() {
		return a;
	}

	public void setA(int a) {
		this.a = a;
	}

	@Override
	public int compareTo(Test o) {
		// TODO Auto-generated method stub
		return a-o.a;
	}

	@Override
	public String toString() {
		return String.format("Test [a=%s]", a);
	}
	
	
	
}